/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-04-19 23:28:09
 */
//导入数据库实例 便于操作数据库
const db = require('../config/db')

//好友操作  添加好友
exports.bulidFriend = (uid, fid, state) => {
    let data = {
        userid: uid,
        friendid: fid,
        state: state,
        time: new Date(),
        lastTime: new Date()
    }
    let sql = 'insert into `friend` set ?'
    //插入到friend表中
    db.query(sql, data, (err, result) => {
        if (err)
            // return res.sendMsg(err, 400)
            console.log('数据库出现错误');
        else if (result.affectedRows != 1) console.log('添加好友失败')
        else {
            // res.sendMsg('success', 200)
        }
    })
}

//修改好友最后通讯时间
exports.updateFriendLastTime = (uid, fid) => {
    let sql = 'update `friend` set lastTime = ? where (userid = ? and friendid = ?) OR (userid = ? and friendid = ?)'
    db.query(sql, [new Date(), uid, fid, fid, uid], (err, result) => {
        if (err) console.log('数据库出现错误'); //return res.sendMsg(err, 400)
        if (result.affectedRows != 2) console.log('修改最后通讯时间失败'); //return res.sendMsg('修改最后通讯时间失败', 400)
        else {
            //res.sendMsg('success', 200)
        }
    })
}

//添加一对一消息
exports.insertMsg = (uid, fid, msg, type, res) => {
    let data = {
        userid: uid,
        friendid: fid,
        message: msg,
        types: type,
        time: new Date(),
        state: 1,  //未读
    }
    let sql = 'insert into `message` set ?'
    //插入到message表中
    db.query(sql, data, (err, result) => {
        if (res) {
            if (err) return res.sendMsg(err, 400)
            if (result.affectedRows != 1) return res.sendMsg('添加消息失败', 400)
            else {
                res.sendMsg('success', 200)
            }
        }
    })

}

//好友申请 
exports.applyFriend = (data, res) => {
    //先查询friend表中是否有申请记录
    let sql = 'select * from `friend` where userid = ? and friendid = ?'
    db.query(sql, [data.uid, data.fid], (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.length == 0) {  //说明是第一次申请
            //调用添加进好友表
            this.bulidFriend(data.uid, data.fid, 2)   //当前用户为申请发送方
            this.bulidFriend(data.fid, data.uid, 1)   //对方好友state为不是好友 （指当前用户)
        } else {  //已经申请过好友 只需修改最后通讯时间即可
            this.updateFriendLastTime(data.uid, data.fid)
        }
        //添加消息
        this.insertMsg(data.uid, data.fid, data.msg, 0, res)

    })
}

//修改好友状态 0为好友 1不是好友 2申请发送方  同意好友
exports.updateFriendState = (uid, fid, res) => {
    let sql = 'update `friend` set state = 0 where userid = ? and friendid = ?'
    db.query(sql, [uid, fid], (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.affectedRows != 1) return res.sendMsg('修改好友状态失败', 400)
        else {
            //res.sendMsg('success', 200)
        }
    })
}

//删除好友|拒绝好友
exports.deleteFriend = (uid, fid, res) => {
    let sql = 'delete from `friend` where userid = ? and friendid = ?'
    db.query(sql, [uid, fid], (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.affectedRows != 1) return res.sendMsg('拒绝|删除好友失败', 400)
        else {
            //res.sendMsg('success', 200)
        }
    })
}

